package com.youdianstar.app.common.base

import android.app.Activity
import androidx.lifecycle.*
import com.youdianstar.app.common.event.EventLiveData
import com.youdianstar.app.common.event.NetStateLiveData


/**
 * File: BaseViewModel
 * Author: 82149 Create: 2021/9/18 17:02
 * Changes (from 2021/9/18)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
open class BaseViewModel : ViewModel(), IBaseViewModel{
    val netLiveData = NetStateLiveData<Any>()
    //尝试两种事件回调

    val loadingLiveData by lazy { EventLiveData<Boolean>() }

    val loadingNetEvent: MutableLiveData<String> by lazy { MutableLiveData<String>() }

    val dismissNetEvent: MutableLiveData<String> by lazy { MutableLiveData<String>() }

    val toastEvent: MutableLiveData<String> by lazy { MutableLiveData<String>() }

    val finishEvent: MutableLiveData<Any> by lazy { MutableLiveData<Any>() }

    val backPressedEvent: MutableLiveData<Any> by lazy { MutableLiveData<Any>() }

    val startActivityEvent: MutableLiveData<Map<String, Any>> by lazy { MutableLiveData<Map<String, Any>>() }

    override fun onAny(owner: LifecycleOwner, event: Lifecycle.Event) {

    }

    override fun onCreate() {

    }

    override fun onDestroy() {

    }

    override fun onStart() {

    }

    override fun onStop() {

    }

    override fun onResume() {

    }

    override fun onPause() {

    }

    /*********************************************************/

    fun showLoading(){
        loadingNetEvent.postValue("")
    }

    fun dismissDialog(){
        dismissNetEvent.postValue("")
    }

    /**
     * 关闭界面
     */
    fun finish() {
        finishEvent.postValue("")
    }

    /**
     * 返回上一层
     */
    fun onBackPressed() {
        backPressedEvent.postValue("")
    }


    /**
     * 页面跳转
     */
    inline fun <reified T : Activity> startActivity(vararg params: Pair<String, Any?>, isToken:Boolean = false) {
        val map = mapOf(CLASS to T::class.java, BUNDLE to params, TOKONCHECK to isToken)
        startActivityEvent.value = map
    }



    companion object {
        val CLASS: String = "CLASS"
        val BUNDLE: String = "BUNDLE"
        val TOKONCHECK:String = "TOKONCHECK"
        val TIPS: String = "TIPS"
        val ACTION: String = "Action"
        val TITLE:String = "TITLE"
        val MSG:String = "MSG"
        val NICKNAME: String = "nickname"
        val ICON: String = "icon"
    }
}